package model;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/7/27
 * 版权所有: admin
 */
public class Model {
    private final static Logger log = LoggerFactory.getLogger(Model.class);
    List<Condition> andConditionList = null;
    List<Condition> orConditionList = null;
    List<String> orderByList = null;

    private List getAndConditionList() {
        if (andConditionList == null) {
            andConditionList = new ArrayList();
        }
        return andConditionList;
    }

    private List getOrConditionList() {
        if (orConditionList == null) {
            orConditionList = new ArrayList();
        }
        return orConditionList;
    }

    private List getOrderByList() {
        if (orderByList == null) {
            orderByList = new ArrayList();
        }
        return orderByList;
    }

    public static Model getInstance() {
        return new Model();
    }

    public Map getValue() {
        Map map = new HashMap();
        if (andConditionList != null) {
            map.put("andCondition", andConditionList);
        }
        if (orConditionList != null) {
            map.put("orCondition", orConditionList);
        }
        if (orderByList != null) {
            map.put("orderBy", orderByList);
        }
        return map;
    }

    //============================================================================
    //============================================================================
    //============================================================================
    private void addCondition(String column, Operator operator, Object value, boolean prepared, boolean type) {
        if (value == null) {
            if (!operator.getValue().equals("IS NOT NULL") && !operator.getValue().equals("IS NULL")) {
              log.warn("is not 或 is 出现非空参数");
                return;
            }
        } else if (value instanceof String && "".equals(value)) {
            return;
        }
        if (type) {
            getAndConditionList().add(new Condition(column, operator.getValue(), value, prepared));
        } else {
            getOrConditionList().add(new Condition(column, operator.getValue(), value, prepared));
        }
    }

    public void addAndCondition(String column, Operator operator, Object value, boolean prepared) {
        addCondition(column, operator, value, prepared, true);
    }

    public void addAndCondition(String column, Operator operator, Object value) {
        addAndCondition(column, operator, value, true);
    }

    public void addOrCondition(String column, Operator operator, Object value, boolean prepared) {
        addCondition(column, operator, value, prepared, false);
    }

    public void addOrCondition(String column, Operator operator, Object value) {
        addOrCondition(column, operator, value, true);
    }


    public void addOrderBy(String column) {
        addOrderBy(column, true);
    }

    public void addOrderBy(String column, Boolean positive) {
        if (positive == false) {
            getOrderByList().add(column + " DESC ");
        } else {
            getOrderByList().add(column);
        }
    }

    //============================================================================
    //============================================================================
    //============================================================================

    public void clear() {
        clearAndConditions();
        clearOrConditions();
        clearOrderBys();
    }

    public void clearAndConditions() {
        if (andConditionList != null) {
            andConditionList.clear();
        }
    }

    public void clearOrConditions() {
        if (orConditionList != null) {
            orConditionList.clear();
        }
    }

    public void clearOrderBys() {
        if (orderByList != null) {
            orderByList.clear();
        }
    }

    public void removeAndCondition(String column) {
        for (Condition condition : andConditionList) {
            if (condition.getColumn().equals(column)) {
                andConditionList.remove(condition);
            }
        }
    }

    public void removeOrCondition(String column) {
        for (Condition condition : orConditionList) {
            if (condition.getColumn().equals(column)) {
                orConditionList.remove(condition);
            }
        }
    }

    public void removeOrderBy(String column) {
        for (String s : orderByList) {
            if (s.indexOf(column) != 0) {
                orderByList.remove(s);
            }
        }
    }


}
